package fu.capstone.hanoicityguide.presentation.fragment.hotel;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.UiLifecycleHelper;

import fu.capstone.hanoicityguide.R;
import fu.capstone.hanoicityguide.business.bo.hotel.HotelBO;
import fu.capstone.hanoicityguide.business.bo.hotel.HotelBOImp;
import fu.capstone.hanoicityguide.business.model.Hotel;
import fu.capstone.hanoicityguide.common.utility.InternetSetting;
import fu.capstone.hanoicityguide.common.utility.SharingFacebook;
import fu.capstone.hanoicityguide.presentation.adapter.ViewPagerAdapter;
import fu.capstone.hanoicityguide.presentation.fragment.home.HomeFragment;

;

@SuppressLint("ValidFragment")
public class DetailHotelFragment extends Fragment {

	private HotelBO hotelBO;
	private int hotelID;
	private ViewPager imgFragment;
	private ViewPagerAdapter vpagerAdapter;
	private TextView hotelDescription;
	private TextView hotelInformation;
	private UiLifecycleHelper uiHelper;
	private InternetSetting internetSetting;
	private Hotel hotel;

	public DetailHotelFragment() {

	}

	public DetailHotelFragment(int hotelID) {
		this.hotelID = hotelID;
	}

	public int getHotelID() {
		return this.hotelID;
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		if (uiHelper != null) {
			uiHelper.onDestroy();
		}
		super.onDestroy();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		setHasOptionsMenu(true);
		internetSetting = new InternetSetting();
		// list attraction by filter and sorter
		View browseHotelFragment = inflater.inflate(
				R.layout.fragment_detail_hotel, container, false);

		hotelBO = new HotelBOImp(container.getContext());

		// get data from database
		hotel = hotelBO.browseHotel(getHotelID());

		// fill data into components
		hotelDescription = (TextView) browseHotelFragment
				.findViewById(R.id.txtHotelDescription);
		hotelInformation = (TextView) browseHotelFragment
				.findViewById(R.id.txtHotelInformation);
		hotelDescription.setText(hotel.getDescription());

		String information = "";
		float x = hotel.getPrice();

		if (!"".equals(String.valueOf(hotel.getPrice()).trim())) {
			information = information.concat("Price : About ");
			information = information.concat(hotel.getPrice() + " VNĐ");
			information = information.concat("\n");
		}
		if (!"".equals(hotel.getHotelClass().trim())) {
			information = information.concat("Class : ");
			information = information.concat(hotel.getHotelClass());
			information = information.concat("\n");
		}
		if (!"".equals(hotel.getAddress().trim())) {
			information = information.concat("Address : ");
			information = information.concat(hotel.getAddress());
			information = information.concat("\n");
		}
		if (!"".equals(hotel.getPhoneNumber().trim())) {
			information = information.concat("Phone number : ");
			information = information.concat(hotel.getPhoneNumber());
			information = information.concat("\n");
		}
		if (!"".equals(hotel.getWebsite().trim())) {
			information = information.concat("Website : ");
			information = information.concat(hotel.getWebsite());
			information = information.concat("\n");
		}

		hotelInformation.setText(information);

		// FragmentActivity fma = (FragmentActivity) getActivity();

		imgFragment = (ViewPager) browseHotelFragment
				.findViewById(R.id.hotelPager);

		vpagerAdapter = new ViewPagerAdapter(getActivity(), hotel.getArrImage());

		imgFragment.setAdapter(vpagerAdapter);

		return browseHotelFragment;
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		// TODO Auto-generated method stub
		inflater = getActivity().getMenuInflater();
		inflater.inflate(R.menu.detailed_hotel_page_menu, menu);
		super.onCreateOptionsMenu(menu, inflater);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		Fragment fragment = new HotelOnlineMapFragment();
		internetSetting = new InternetSetting(getActivity());
		switch (item.getItemId()) {
		case R.id.action_map: {

			if (internetSetting.getConnectivityStatus(getActivity())) {
				fragment = new HotelOnlineMapFragment();
				Bundle bundle = new Bundle();
				bundle.putInt("HotelID", hotelID);
				fragment.setArguments(bundle);
				if (fragment != null) {
					FragmentManager fragmentManager = getFragmentManager();
					FragmentTransaction fTransaction = fragmentManager
							.beginTransaction();
					fTransaction.addToBackStack(null);
					fTransaction.replace(R.id.frame_container, fragment)
							.commit();
					getActivity().setTitle("View on map");
				}
			}

			if (!internetSetting.getConnectivityStatus(getActivity())
					&& HomeFragment.isPurchased) {
				fragment = new HotelOfflineMapFragment();
				Bundle bundle = new Bundle();
				bundle.putInt("HotelID", hotelID);
				fragment.setArguments(bundle);
				if (fragment != null) {
					FragmentManager fragmentManager = getFragmentManager();
					FragmentTransaction fTransaction = fragmentManager
							.beginTransaction();
					fTransaction.addToBackStack(null);
					fTransaction.replace(R.id.frame_container, fragment)
							.commit();
					getActivity().setTitle("View on map");
				}
			}

			if (!internetSetting.getConnectivityStatus(getActivity())
					&& !HomeFragment.isPurchased) {
				internetSetting.showSettingsAlert();
			}
		}
			break;
		case R.id.action_checkin: {
			if (internetSetting.getConnectivityStatus(getActivity())) {
				uiHelper = new UiLifecycleHelper(getActivity(), null);
				SharingFacebook.shareFacebook(uiHelper, getActivity());
			} else {
				internetSetting.showSettingsAlert();
			}
		}
			break;
		case R.id.action_contact: {
			if (hotel != null
					&& (!hotel.getPhoneNumber().trim().equals("") || !hotel
							.getWebsite().trim().equals(""))) {

				AlertDialog.Builder alertDialog = new AlertDialog.Builder(
						getActivity());

				// Setting Dialog Title
				alertDialog.setTitle("Contact to " + hotel.getName());

				// Setting Dialog Message
				alertDialog.setMessage("Do you want to contact with this "
						+ hotel.getType() + " ?");

				if (!hotel.getPhoneNumber().trim().equals("")) {
					// On pressing Call button
					alertDialog.setPositiveButton("Call",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									Intent intent = new Intent(
											Intent.ACTION_CALL);
									String link = hotel.getWebsite();
									if (!link.startsWith("http://")
											&& !link.startsWith("https://")) {
										link = "http://" + link;
									}
									intent.setData(Uri.parse(link));
									startActivity(intent);
								}
							});
				}

				if (!hotel.getWebsite().trim().equals("")) {
					alertDialog.setNeutralButton("Go to Website",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									Intent intent = new Intent(
											Intent.ACTION_VIEW);
									intent.setData(Uri.parse(hotel.getWebsite()));
									getActivity().startActivity(intent);
								}
							});
				}

				// on pressing cancel button
				alertDialog.setNegativeButton("Cancel",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
								dialog.cancel();
							}
						});

				// Showing Alert Message
				alertDialog.show();

			} else {
				Toast.makeText(getActivity(), "No contact information !",
						Toast.LENGTH_SHORT).show();
			}

			break;
		}
		default:
			break;
		}

		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		getActivity().getActionBar().setTitle(
				hotelBO.browseHotel(hotelID).getName());
	}
}
